package com.onlygx.dva.web.base.service;

import cn.hutool.core.util.StrUtil;
import com.onlygx.dva.common.properties.CommonCoreProperties;
import com.onlygx.dva.common.properties.CommonMinioOSSProperties;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

/**
 * <p>
 *  Minio oss 服务
 * </p>
 *
 * @author 高祥
 * @since 2023-07-28
 */
@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(value = "dva-core.use-oss", havingValue = "minio")
public class MinioOSSService implements OSSService{

    final CommonCoreProperties commonCoreProperties;

    final CommonMinioOSSProperties commonMinioOSSProperties;

    private MinioClient minioClient;


    @PostConstruct
    public void init() {
        try {
            log.info("初始化Minio OSS服务");
            minioClient = MinioClient.builder()
                    .endpoint(new URL(commonMinioOSSProperties.getEndpoint()))
                    .credentials(commonMinioOSSProperties.getAccessKey()
                            , commonMinioOSSProperties.getSecretKey())
                    .build();
            //验证
            boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(commonMinioOSSProperties.getBucketName())
                    .build());
            //不存在则创建
            if (!isExist) {
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(commonMinioOSSProperties.getBucketName())
                        .build());
            }

        } catch (Exception e) {
            log.error("Minio OSS初始化失败，原因：{}",e.toString());
            //e.printStackTrace();
            //throw new RuntimeException(e);
        }
    }

    @Override
    public String pushFile(File file) {
        return pushFile(file, null);
    }

    @Override
    public String pushFile(String name, InputStream inputStream) {
        return pushFile(name, null, inputStream);
    }

    @Override
    public String pushFile(File file, String dir) {
        try {
            String fileName = commonMinioOSSProperties.getBaseDir() + "/";
            if (StrUtil.isEmpty(dir)){
                fileName += file.getName();
            }else{
                fileName += dir + "/" + file.getName();
            }
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(commonMinioOSSProperties.getBucketName())
                    .object(fileName)
                    .stream(new FileInputStream(file), -1, 10485760)
                    .build());
            return commonMinioOSSProperties.getPubEndpoint()
                    + "/"
                    + commonMinioOSSProperties.getBucketName()
                    + "/"
                    + fileName;
        } catch (Exception e){
            throw  new NullPointerException("minio push file error1:" + e.getMessage());
        }
    }

    @Override
    public String pushFile(String name, String dir, InputStream inputStream) {
        try {
            String fileName = commonMinioOSSProperties.getBaseDir() + "/";
            if (StrUtil.isEmpty(dir)){
                fileName += name;
            }else{
                fileName += dir + "/" + name;
            }
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(commonMinioOSSProperties.getPubEndpoint() )
                    .object(fileName)
                    .stream(inputStream, -1, 10485760)
                    .build());
            return commonMinioOSSProperties.getPubEndpoint()
                    + "/"
                    + commonMinioOSSProperties.getBucketName()
                    + "/"
                    + fileName;
        } catch (Exception e){
            throw  new NullPointerException("minio push file error2:" + e.getMessage());
        }
    }

}
